home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 5 / BBS in a Box -Volume V (BBS in a Box) (April 1992).iso / Files / Prog / M / MPWGCC (Machines).cpt / Miscellaneous / gctool.c / gctool.c
Encoding:
C/C++ Source or Header  |  1990-11-16  |  8.6 KB  |  405 lines  |  [TEXT/MPS ]

  1. /*
  2.     C driver.c
  3.     link driver.c.o -o driver -t MPST -c 'MPS ' ∂
  4.         "{clibraries}"cruntime.o "{clibraries}"stdclib.o ∂
  5.         "{clibraries}"cinterface.o "{libraries}"interface.o
  6.  
  7. */
  8.  
  9. #include <types.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12.  
  13. #define MAXFILENAME 255
  14. #define MAXOPTIONS 1023
  15.  
  16. int    run_cpp = 1;
  17. int run_cc1 = 1;
  18. int run_asm = 1;
  19. int optimize = 1;
  20. int quiet_flag = 1;
  21. int echo_cmds = 0;
  22.  
  23. int got_mc68020 = 0;
  24.  
  25. char* output_filename;
  26. char* input_filename;
  27. char* toolplace;
  28. char* tmp;
  29.  
  30. char cpp_options[MAXOPTIONS];
  31. char cc1_options[MAXOPTIONS];
  32. char asm_options[MAXOPTIONS];
  33.  
  34. char tmp_filename[MAXFILENAME];
  35. char cpp_filename[MAXFILENAME];
  36. char asm_filename[MAXFILENAME];
  37. char out_filename[MAXFILENAME];
  38.  
  39. main (argc, argv)
  40. int argc;
  41. char* argv[];
  42. {
  43.     int i, badopt;
  44.     char* arg;
  45.     char* missing;
  46.     char* basename;
  47.     
  48.     output_filename = "";
  49.     input_filename = "";
  50.     toolplace = "";
  51.     tmp = ":";
  52.     
  53.     strcpy(cpp_options, "-D__GNUC__");
  54.     cc1_options[0] = '\0';
  55.     strcpy(asm_options, "-w");
  56.     
  57.     for (i=1; i<argc; i++)
  58.     {
  59.         arg = argv[i];
  60.         badopt = 0;
  61.         missing = "";
  62.         
  63.         if (arg[0] == '-') {
  64.             switch(arg[1]) {
  65.             
  66.             case 'a':
  67.                 if (strcmp(arg, "-ansi") == 0)
  68.                     strcat(cc1_options, " -ansi");
  69.                 else if (strcmp(arg, " -asm") == 0)
  70.                     run_asm = 0;
  71.                 else
  72.                     badopt = 1;
  73.                 break;
  74.                 
  75.             case 'b':
  76.                 if (arg[2] == '\0')
  77.                     strcat(cc1_options, " -mb");
  78.                 else if ( (arg[2] == '2' || arg[2] == '3') && arg[3] == '\0' )
  79.                     fprintf(stderr, "Warning: %s ignored\n", arg);
  80.                 else
  81.                     badopt = 1;
  82.                 break;
  83.                 
  84.             case 'c':
  85.                 if (arg[2] == '\0')
  86.                     run_cc1 = 0;
  87.                 else
  88.                     badopt = 1;
  89.                 break;
  90.                 
  91.             case 'd':
  92.                 if (arg[2] == '\0') {
  93.                     if (++i<argc) {
  94.                         strcat(cpp_options, " -D");
  95.                         strcat(cpp_options, argv[i]);
  96.                     }
  97.                     else
  98.                         missing = "symbol";
  99.                 }
  100.                 else {
  101.                     strcat(cc1_options, " ");
  102.                     strcat(cc1_options, arg);
  103.                 }
  104.                 break;
  105.                 
  106.             case 'e':
  107.                 if (arg[2] == '\0') {
  108.                     strcat(cpp_options, " -C");
  109.                     run_cc1 = 0;
  110.                 }
  111.                 else if (arg[2] == '2' && arg[3] == '\0')
  112.                     run_cc1 = 0;
  113.                 else if (strcmp(arg, "-elems881") == 0)
  114.                     strcat(cc1_options, " -melems881");
  115.                 else
  116.                     badopt = 1;
  117.                 break;
  118.                 
  119.             case 'f':
  120.                 if (arg[2] == 'x' && arg[3] == '\0') {
  121.                     if (++i>=argc)
  122.                         missing = "number";
  123.                     else if (strcmp(argv[i], "30") == 0)
  124.                         strcat(cc1_options, " -mfx30");
  125.                     else
  126.                         fprintf(stderr, "Warning: -fx %s ignored.\n", argv[i]);
  127.                 }
  128.                 else {
  129.                     strcat(cc1_options, " ");
  130.                     strcat(cc1_options, arg);
  131.                 }
  132.                 break;
  133.                 
  134.             case 'i':
  135.                 if (arg[2] == '\0') {
  136.                     if (++i<argc) {
  137.                         strcat(cpp_options, " '-I");
  138.                         strcat(cpp_options, argv[i]);
  139.                         strcat(cpp_options, "'");
  140.                     }
  141.                     else
  142.                         missing = "directory";
  143.                 }
  144.                 else
  145.                     badopt = 1;
  146.                 break;
  147.                 
  148.             case 'm':
  149.                 if (arg[2] == '\0') {
  150.                     strcat(cc1_options, " -mm");
  151.                     if (got_mc68020 == 0) {
  152.                         fprintf(stderr, "Warning: -m implies -mc68020\n");
  153.                         strcat(cpp_options, " -Dmc68020");
  154.                         strcat(cc1_options, " -mc68020");
  155.                         got_mc68020 = 1;
  156.                     }
  157.                 }
  158.                 else if (strcmp(arg, "-makedep") == 0) {
  159.                     strcat(cpp_options, " -M");
  160.                     run_cc1 = 0;
  161.                 }
  162.                 else if (strcmp(arg, "-mbg") == 0) {
  163.                     if (++i<argc) {
  164.                         if (strcmp(argv[i], "off") == 0)
  165.                             strcat(cc1_options, " -mbgoff -fomit-frame-pointer");
  166.                         else if (strcmp(argv[i], "full") != 0)
  167.                             fprintf(stderr, "Warning: only -mbg off and -mbg full are supported.\n");
  168.                     }
  169.                     else
  170.                         missing = "\"full\" or \"off\"";
  171.                 }
  172.                 else if (strcmp(arg, "-mc68020") == 0) {
  173.                     if (got_mc68020 == 0) {
  174.                         strcat(cpp_options, " -Dmc68020");
  175.                         strcat(cc1_options, " -mc68020");
  176.                         got_mc68020 = 1;
  177.                     }
  178.                 }
  179.                 else if (strcmp(arg, "-mc68881") == 0) {
  180.                     strcat(cpp_options, " -Dmc68881");
  181.                     strcat(cc1_options, " -mc68881");
  182.                 }
  183.                 else if (strcmp(arg, "-mnoseg") == 0)
  184.                     strcat(cc1_options, " -mnoseg");
  185.                 else
  186.                     badopt = 1;
  187.                 break;
  188.                 
  189.             case 'n':
  190.                 if (strcmp(arg, "-no") == 0)
  191.                     optimize = 0;
  192.                 else if (arg[2] == '\0')
  193.                     fprintf(stderr, "Warning: -n ignored.\n");
  194.                 else
  195.                     badopt = 1;
  196.                 break;
  197.                 
  198.             case 'o':
  199.                 if (arg[2] == '\0') {
  200.                     if (++i<argc)
  201.                         output_filename = argv[i];
  202.                     else
  203.                         missing = "output filename";
  204.                 }
  205.                 else
  206.                     badopt = 1;
  207.                 break;
  208.  
  209.             case 'p':
  210.                 if (arg[2] == '\0') {
  211.                     strcat(cpp_options, " -v");
  212.                     strcat(cc1_options, " -version");
  213.                     quiet_flag = 0;
  214.                     strcat(asm_options, " -p");
  215.                     echo_cmds = 1;
  216.                 }
  217.                 else if (strcmp(arg, "-pedantic") == 0) {
  218.                     strcat(cpp_options, " -pedantic");
  219.                     strcat(cc1_options, " -pedantic");
  220.                 }
  221.                 else
  222.                     badopt = 1;
  223.                 break;
  224.                 
  225.             case 'r':
  226.                 if (arg[2] == '\0')
  227.                     strcat(cc1_options, " -Wimplicit");
  228.                 else
  229.                     badopt = 1;
  230.                 break;
  231.                 
  232.             case 's':
  233.                 if (arg[2] == '\0') {
  234.                     if (++i<argc) {
  235.                         strcat(cc1_options, " -s '");
  236.                         strcat(cc1_options, argv[i]);
  237.                         strcat(cc1_options, "'");
  238.                     }
  239.                     else
  240.                         missing = "segment name";
  241.                 }
  242.                 else if (strcmp(arg, "-sym") == 0)
  243.                     if (++i<argc) {
  244.                         strcat(asm_options, " -sym ");
  245.                         strcat(asm_options, argv[i]);
  246.                         fprintf(stderr, "Warning: symbolic info will be produced by assembler.\n");
  247.                     }
  248.                     else
  249.                         missing = "option";
  250.                 else
  251.                     badopt = 1;
  252.                 break;
  253.                 
  254.             case 't':
  255.                 if (arg[2] == '\0') {
  256.                     quiet_flag = 0;
  257.                     strcat(asm_options, " -t");
  258.                 }
  259.                 else if (strcmp(arg, "-tools") == 0) {
  260.                     if (++i<argc)
  261.                         toolplace = argv[i];
  262.                     else
  263.                         missing = "directory";
  264.                 }
  265.                 else if (strcmp(arg, "-traditional") == 0) {
  266.                     strcat(cpp_options, " -traditional");
  267.                     strcat(cc1_options, " -traditional");
  268.                 }
  269.                 else
  270.                     badopt = 1;
  271.                 break;
  272.                 
  273.             case 'u':
  274.                 if (arg[2] == '\0') {
  275.                     if (++i<argc) {
  276.                         strcat(cpp_options, " -U");
  277.                         strcat(cpp_options, argv[i]);
  278.                     }
  279.                     else
  280.                         missing = "symbol";
  281.                 }
  282.                 else
  283.                     badopt = 1;
  284.                 break;
  285.                 
  286.             case 'w':
  287.                 if (arg[2] == '\0')
  288.                     strcat(cc1_options, " -w");
  289.                 else if (arg[2] == '2' && arg[3] == '\0')
  290.                     strcat(cc1_options, " -Wall");
  291.                 else {
  292.                     strcat(cc1_options, " ");
  293.                     strcat(cc1_options, arg);
  294.                 }
  295.                 break;
  296.                 
  297.             case 'y':
  298.                 if (arg[2] == '\0') {
  299.                     if (++i<argc)
  300.                         tmp = arg;
  301.                     else
  302.                         missing = "directory";
  303.                 }
  304.                 else
  305.                     badopt = 1;
  306.                 break;
  307.  
  308.             default:
  309.                 badopt = 1;
  310.             }
  311.             
  312.             if (missing[0] != '\0')
  313.                 fprintf(stderr, "Warning: missing %s after \"%s\".\n", missing, arg);
  314.             else if (badopt)
  315.                 fprintf(stderr, "Warning: invalid option %s ignored.\n", arg);
  316.         }
  317.         
  318.         else if (input_filename[0] == '\0')
  319.             input_filename = arg;
  320.             
  321.         else
  322.             fprintf(stderr, "Warning input filename %s ignored.\n", arg);
  323.     }
  324.     
  325.     if (input_filename[0] == '\0') {
  326.         fprintf(stderr, "Error: no input filename.\n");
  327.         exit(2);
  328.     }
  329.     
  330.     strcpy(tmp_filename, tmp);
  331.     if ( (basename = strrchr(input_filename, ':')) != NULL)
  332.         strcat(tmp_filename, basename);
  333.     else
  334.         strcat(tmp_filename, input_filename);
  335.     
  336.     if (run_cc1 == 1) {
  337.         strcpy(cpp_filename, tmp_filename);
  338.         strcat(cpp_filename, ".cpp");
  339.     }
  340.     
  341.     strcpy(asm_filename, tmp_filename);
  342.     strcat(asm_filename, ".a");
  343.     
  344.     if (output_filename[0] == '\0') {
  345.         strcpy(out_filename, input_filename);
  346.         strcat(out_filename, ".o");
  347.         output_filename = out_filename;
  348.     }
  349.         
  350.     if (quiet_flag == 1)
  351.         strcat(cc1_options, " -quiet");
  352.         
  353.     if (optimize == 1)
  354.         strcat(cc1_options, " -O");
  355.     
  356.     printf("set oldexit {exit}\n");
  357.     printf("set exit 0\n");
  358.     
  359.     if (echo_cmds == 1) {
  360.         printf("set oldecho {echo}\n");
  361.         printf("set echo 1\n");
  362.     }
  363.         
  364.     if (run_cpp == 1) {
  365.         printf("'%s'cpp %s %s %s\n", toolplace, cpp_options, input_filename, cpp_filename);
  366.         printf("if {status} == 0\n");
  367.         
  368.         if (run_cc1 == 1) {
  369.             printf("\t'%s'cc1 %s %s -o %s -dumpbase %s\n", 
  370.                             toolplace, cc1_options, cpp_filename, asm_filename, tmp_filename);
  371.             printf("\tif {status} == 0\n");
  372.             printf("\t\tdelete -i -y %s\n", cpp_filename);
  373.             
  374.             if (run_asm == 1) {
  375.                 printf("\t\tasm %s %s -o %s\n", asm_options, asm_filename, output_filename);
  376.                 printf("\t\tif {status} == 0\n");
  377.                 printf("\t\t\tdelete -i -y %s\n", asm_filename);
  378.                 printf("\t\telse\n");
  379.                 printf("\t\t\tset exit {oldexit}\n");
  380.                 printf("\t\t\texit 2\n");
  381.                 printf("\t\tend\n");
  382.             }
  383.             
  384.             printf("\telse\n");
  385.             printf("\t\tdelete -i -y %s\n", cpp_filename);
  386.             printf("\t\tdelete -i -y %s\n", asm_filename);
  387.             printf("\t\tset exit {oldexit}\n");
  388.             printf("\t\texit 2\n");
  389.             printf("\tend\n");
  390.             
  391.         }
  392.         printf("else\n");
  393.         printf("\tdelete -i -y %s\n", cpp_filename);
  394.         printf("\tset exit {oldexit}\n");
  395.         printf("\texit 2\n");
  396.         printf("end\n");
  397.     }
  398.     
  399.     if (echo_cmds == 1)
  400.         printf("set echo {oldecho}\n");
  401.     printf("set exit {oldexit}\n");
  402.     
  403.     exit(0);
  404. }
  405.